﻿using System;
using System.Xml;

namespace Graphe.Collations
{
    public class Note : ICollationElement, IComparable<Note>
    {
        #region Public Properties

        public string Text { get; set; }
        public string Manuscript { get; set; }

        #endregion

        public void Parse(XmlNode noteNode)
        {
            Text = noteNode.InnerText;

            if (noteNode.Attributes["Manuscript"] != null)
                Manuscript = noteNode.Attributes["Manuscript"].Value;
        }


        #region ICollationElement Members

        public void Parse(XmlReader reader, CollationDebugMessages debugMessages)
        {
            if (reader.Name != "Note")
            {
                debugMessages.Add(String.Format("Unexpected xml parsing note: {0}.", reader.ReadOuterXml()));
            }
            else
            {
                if (reader.MoveToFirstAttribute())
                {
                    do
                    {
                        if (reader.Name == "Manuscript")
                        {
                            Manuscript = reader.ReadContentAsString();
                        }
                        else
                        {
                            debugMessages.Add(String.Format("Unknown attribute on note node: {0}, Value {1}.", reader.Name, reader.ReadContentAsString()));
                        }
                    } while (reader.MoveToNextAttribute());
                }

                reader.Read();
                Text = reader.ReadContentAsString();
            }
        }

        public void Render(XmlTextWriter writer)
        {
            writer.WriteStartElement("Note");

            if (!String.IsNullOrEmpty(Manuscript))
                writer.WriteAttributeString("Manuscript", Manuscript);

            writer.WriteString(Text);
            writer.WriteEndElement();
        }

        #endregion

        #region IComparable<Note> Members

        public int CompareTo(Note other)
        {
            if (Text != other.Text || Manuscript != other.Manuscript)
                return -1;
            else
                return 0;
        }

        #endregion
    }
}
